package com.ins.service.impl;


import com.google.common.collect.Lists;
import com.ins.common.exception.ServerException;
import com.ins.common.utils.InsUtils;
import com.ins.common.utils.SecureUser;
import com.ins.mapper.AppOrderMapper;
import com.ins.model.dto.app.AppOrderDTO;
import com.ins.model.dto.app.AppOrderItemDTO;
import com.ins.model.entity.AppInfoMarket;
import com.ins.model.entity.AppOrder;
import com.ins.model.entity.AppOrderItem;
import com.ins.model.vo.OrderPayVO;
import com.ins.service.AppInfoMarketService;
import com.ins.service.AppOrderItemService;
import com.ins.service.AppOrderService;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.dromara.hutool.core.bean.BeanUtil;
import org.dromara.hutool.core.data.id.IdUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 订单表 服务层实现。
 *
 * @author ins
 * @since 2024-05-14
 */
@Service
@RequiredArgsConstructor
public class AppOrderServiceImpl extends ServiceImpl<AppOrderMapper, AppOrder> implements AppOrderService {

    private final AppInfoMarketService appInfoMarketService;

    private final AppOrderItemService appOrderItemService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public OrderPayVO create(AppOrderDTO dto) {
        long orderId = IdUtil.getSnowflakeNextId();
        String orderNo = InsUtils.getNoPrefix(InsUtils.ORDER_NO_PREFIX) + orderId;
        //查询所有sku信息
        Map<Long, AppInfoMarket> queryItemMap = appInfoMarketService
                .listByIds(dto.getItems().stream().map(AppOrderItemDTO::getInfoId).collect(Collectors.toList()))
                .stream().collect(Collectors.toMap(AppInfoMarket::getId, it -> it));
        BigDecimal orderTotalAmount = BigDecimal.ZERO;
        List<AppOrderItem> orderItems = Lists.newArrayList();
        for (AppOrderItemDTO item : dto.getItems()) {
            if (!queryItemMap.containsKey(item.getInfoId())) {
                throw ServerException.spe("信息不存在");
            }
            AppInfoMarket info = queryItemMap.get(item.getInfoId());
            if (info.getDeleted() == 1) {
                throw ServerException.spe("信息" + info.getName() + "已删除");
            }
            BigDecimal itemTotalAmount = info.getPrice().multiply(BigDecimal.valueOf(item.getNum()));
            AppOrderItem appOrderItem = new AppOrderItem();
            BeanUtil.copyProperties(item, appOrderItem);
            appOrderItem.setOrderId(orderId);
            appOrderItem.setOrderNo(orderNo);
            appOrderItem.setPrice(info.getPrice());
            appOrderItem.setTotalPrice(itemTotalAmount);
            appOrderItem.setDeleted(0);
            orderTotalAmount = orderTotalAmount.add(itemTotalAmount);
        }
        AppOrder appOrder = new AppOrder();
        BeanUtil.copyProperties(dto, appOrder);
        appOrder.setId(orderId);
        appOrder.setUserId(SecureUser.getUserId());
        appOrder.setOrderNo(orderNo);
        appOrder.setTotalAmount(orderTotalAmount);
        appOrder.setPayAmount(orderTotalAmount);
        appOrder.setPromotionAmount(BigDecimal.ZERO);
        appOrder.setIntegrationAmount(BigDecimal.ZERO);
        appOrder.setCouponAmount(BigDecimal.ZERO);
        appOrder.setDiscountAmount(BigDecimal.ZERO);
        appOrder.setPayType(0);
        appOrder.setStatus(0);
        appOrder.setIntegration(0);
        appOrder.setGrowth(0);
        appOrder.setDeleted(0);
        this.save(appOrder);
        appOrderItemService.saveBatch(orderItems);
        return new OrderPayVO(orderId, orderTotalAmount);
    }

}